import 'dart:io';

import 'package:flutter/material.dart';
import 'package:filemanager/Common.dart';
import 'package:filemanager/file_manager.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:intl/date_symbol_data_local.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  // 获取手机存储根路径
  Future<void> getSDCardDir() async {
//    Common().sdCardDir = (await getExternalStorageDirectory()).path;
    Common().sdCardDir = '/storage/emulated/0/';
    print('sdCardDir:${Common().sdCardDir}');
  }

  // 权限检查
  Future<void> getPermission() async {
    if (Platform.isAndroid) {
      // 获取存储权限状态
      PermissionStatus permissionStatus = await new PermissionHandler()
          .checkPermissionStatus(PermissionGroup.storage);
      // 检查是否被授权
      if (permissionStatus != PermissionStatus.granted) {
        // 没有则请求授权
        await PermissionHandler().requestPermissions([PermissionGroup.storage]);
      }
      await getSDCardDir();
    } else if (Platform.isIOS) {
      await getSDCardDir();
    }
  }

  Future.wait([initializeDateFormatting("zh_CN", null), getPermission()])
      .then((result) {
    runApp(MyApp());
  });
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'File Manager',
      theme: new ThemeData(primarySwatch: Colors.blue),
      home: new FileManager(),
    );
  }
}
